class LogUtils {
  //是否输入日志标识
  static bool _isLog = true;
  static String _logFlag = "Flutter-Demo";

  static void init({bool islog = false, String logFlag = "Flutter-Demo"}) {
    _isLog = islog;
    _logFlag = logFlag;
  }

  static void e(String message, StackTrace trace) {
    if (_isLog) {
      FCustomTrace programInfo = FCustomTrace(trace);
      if (programInfo != null &&
          programInfo.fileName != null &&
          programInfo.lineNumber != null) {
        print(
            "${LogUtils._logFlag} | ${programInfo.fileName}|${programInfo.lineNumber} :$message");
      } else {
        print("打印内容:$message");
      }
    }
  }
}

class FCustomTrace {
  final StackTrace _trace;

  String fileName;
  int lineNumber;
  int columnNumber;

  FCustomTrace(this._trace) {
    _parseTrace();
  }

  void _parseTrace() {
    var traceString = this._trace.toString().split("\n")[0];
    var indexOfFileName = traceString.indexOf(RegExp(r'[A-Za-z_]+.dart'));
    var fileInfo = traceString.substring(indexOfFileName);
    var listOfInfos = fileInfo.split(":");
    //print('-----listOfInfos--===== $listOfInfos  -------=====');
    this.fileName = listOfInfos[0];
    this.lineNumber = int.parse(listOfInfos[1]);
    var columnStr = listOfInfos[2];
    columnStr = columnStr.replaceFirst(")", "");
    this.columnNumber = int.parse(columnStr);
  }
}
